define(['jquery', 'bootstrap', 'backend', 'addtabs', 'table', 'echarts', 'echarts-theme', 'template','form','xlsx'], function ($, undefined, Backend, Datatable, Table, Echarts, undefined, Template,Form,XLSX) {
    let exportDataList = null;
    let exportDataList2 = null;
    let exportDataList3 = null;
    let exportDataList4 = null;
    let exportDataList5 = null;
    let exportDataList6 = null;
    let exportDataList7 = null;
    var Controller = {
        index: function () {
            

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    tableuser_url: 'dashboard/tableuser',
                    add_url: 'command/add',
                    edit_url: '',
                    del_url: 'command/del',
                    multi_url: 'command/multi',
                    table: 'command',
                }
            });

            var tableuser = $("#tableuser");
            // 初始化表格
            tableuser.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.tableuser_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                commonSearch:false,
                pagination:false,
                columns: [
                    [
                        {
                            field: 'date',
                            title: '日期',
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.date
                        },
                        {field: 'totalcount', title:'用户量',operate:false},
                        {field: 'count', title:'新增用户',operate:false}
                    ]
                ],
                queryParams:function (params) {
                    var time = $("#tableuserform").find("input[name='datetimerange']").val();
                    if (time) {
                        params.between = time;
                    }
                    return params;
                }
            });


            // 为表格绑定事件
            Table.api.bindevent(tableuser);
            Form.events.daterangepicker($(".dashboard"));
            $("#tableuser-search-btn").on("click", function (e) { 
                tableuser.bootstrapTable('refresh');
            });

            
            $("#echartorder-search-btn").on("click", function (e) { 
                setOrderChart(myChart);
            });
            $("#echartorder-export-btn").on("click", function (e) { 
                exportData(exportDataList,'医院订单数量统计');
            });

            $("#echartorderprice-search-btn").on("click", function (e) { 
                setOrderChart2(myChart2);
            });
            $("#echartorderprice-export-btn").on("click", function (e) { 
                exportData2(exportDataList2,'平台订单金额统计');
            });

            $("#recharge-search-btn").on("click", function (e) { 
                setOrderChart3(myChart3);
            });
            $("#recharge-export-btn").on("click", function (e) { 
                exportData2(exportDataList3,'充值金额统计');
            });

            $("#sale-search-btn").on("click", function (e) { 
                setOrderChart5(myChart5);
            });
            $("#sale-export-btn").on("click", function (e) { 
                exportData(exportDataList5,'销售业绩统计');
            });

            $("#saleorder-search-btn").on("click", function (e) { 
                setOrderChart5(myChart7);
            });
            $("#saleorder-export-btn").on("click", function (e) { 
                exportData(exportDataList7,'销售订单统计');
            });

            $("#goods-search-btn").on("click", function (e) { 
                setOrderChart6(myChart6);
            });
            $("#goods-export-btn").on("click", function (e) { 
                exportData(exportDataList6,'项目下单统计');
            });


            // 基于准备好的dom，初始化echarts实例
            var myChart = Echarts.init(document.getElementById('echart'), 'walden');
            setOrderChart(myChart);

            var myChart2 = Echarts.init(document.getElementById('echart2'), 'walden');
            setOrderChart2(myChart2);

            var myChart3 = Echarts.init(document.getElementById('echart3'), 'walden');
            setOrderChart3(myChart3);

            var myChart4 = Echarts.init(document.getElementById('echart4'), 'walden');
            setOrderChart4(myChart4);

            var myChart5 = Echarts.init(document.getElementById('echart5'), 'walden');
            setOrderChart5(myChart5);

            var myChart7 = Echarts.init(document.getElementById('echart7'), 'walden');
            setOrderChart7(myChart7);

            var myChart6 = Echarts.init(document.getElementById('echart6'), 'walden');
            setOrderChart6(myChart6);

            $(window).resize(function () {
                myChart.resize();
                myChart2.resize();
                myChart3.resize();
                myChart4.resize();
                myChart5.resize();
                myChart6.resize();
                myChart7.resize();
            });

        }
    };

    function exportData(data,filename) { 
        let list = [];
        let header = ['日期'];
        data.data.forEach(item => { 
            header.push(item.title);
        });
        list.push(header);
        data.column.forEach((date,index) => { 
            let row = [date];
            
            data.data.forEach((item,key)=>{
                row.push(item.data[index]);
            })
            list.push(row);
        });
       
        exportToExcel(list,filename)
        
    }

    function exportData2(data,filename,header=null) { 
        let list = [];
        list.push(header || ['日期','金额']);
        data.column.forEach((date,index) => { 
            let item = data.data[index];
            let row = [date,item];
            list.push(row);
        });
       
        exportToExcel(list,filename)
        
    }
    function setOrderChart(myChart) {

        // 指定图表的配置项和数据
        var option = {
            title: {
                text: '平台订单量',
                subtext: '统计各医院订单数量',
                top:'0px'
            },
            color: [
                "#18d1b1",
                "#3fb1e3",
                "#626c91",
                "#a0a7e6",
                "#c4ebad",
                "#96dee8"
            ],
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                type: 'scroll',
                top:'60px',
                data: []
            },
            toolbox: {
                show: true,
                feature: {
                    magicType: { show: true, type: ['stack', 'tiled'] },
                    saveAsImage: { show: true }
                }
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: []
            },
            yAxis: {},
            grid: [{
                left: 'left',
                top: 100,
                right: '10',
                bottom: 30
            }],
            series: []
        };

        $.ajax({
            url: 'dashboard/ordercount',
            type: 'POST',
            dataType: 'json',
            data:{
                between: $("#echartorderform").find("input[name='datetimerange']").val()
            },
            success: function (data) {
                console.log(data);
                exportDataList = data;
                let series = [];
                data.data.forEach(item => {
                    option.legend.data.push(item.title);
                    series.push({
                        name: item.title,
                        type: 'line',
                        smooth: true,
                        areaStyle: {
                            normal: {}
                        },
                        lineStyle: {
                            normal: {
                                width: 1.5
                            }
                        },
                        data: item.data
                    })
                })
                option.xAxis.data = data.column;
                option.series = series;
                
                myChart.setOption(option);
            }
        })
    }


    function setOrderChart2(myChart) {

        // 指定图表的配置项和数据
        var option = {
            title: {
                text: '平台订单总额',
                subtext: '平台订单总额'
            },
            color: [
                "#18d1b1",
                "#3fb1e3",
                "#626c91",
                "#a0a7e6",
                "#c4ebad",
                "#96dee8"
            ],
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                type: 'scroll',
                data: []
            },
            toolbox: {
                show: true,
                feature: {
                    magicType: { show: true, type: ['stack', 'tiled'] },
                    saveAsImage: { show: true }
                }
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: []
            },
            yAxis: {},
            grid: [{
                left: 'left',
                top: 60,
                right: '10',
                bottom: 30
            }],
            series: []
        };

        $.ajax({
            url: 'dashboard/orderprice',
            type: 'POST',
            dataType: 'json',
            data:{
                between: $("#echartorderpriceform").find("input[name='datetimerange']").val()
            },
            success: function (data) {
                exportDataList2 = data;
                
                let series = [{
                        name: '总额',
                        type: 'line',
                        smooth: true,
                        areaStyle: {
                            normal: {}
                        },
                        lineStyle: {
                            normal: {
                                width: 1.5
                            }
                        },
                        data: data.data
                    }];

                option.xAxis.data = data.column;
                option.series = series;
                
                myChart.setOption(option);
            }
        })
    }


    function setOrderChart3(myChart) {

        // 指定图表的配置项和数据
        var option = {
            title: {
                text: '平台充值金额',
                subtext: '平台充值金额'
            },
            color: [
                "#18d1b1",
                "#3fb1e3",
                "#626c91",
                "#a0a7e6",
                "#c4ebad",
                "#96dee8"
            ],
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                type: 'scroll',
                data: []
            },
            toolbox: {
                show: true,
                feature: {
                    magicType: { show: true, type: ['stack', 'tiled'] },
                    saveAsImage: { show: true }
                }
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: []
            },
            yAxis: {},
            grid: [{
                left: 'left',
                top: 60,
                right: '10',
                bottom: 30
            }],
            series: []
        };

        $.ajax({
            url: 'dashboard/rechargeprice',
            type: 'POST',
            dataType: 'json',
            data:{
                between: $("#rechargeform").find("input[name='datetimerange']").val()
            },
            success: function (data) {
                exportDataList3 = data;
                let series = [{
                        name: '金额',
                        type: 'line',
                        smooth: true,
                        areaStyle: {
                            normal: {}
                        },
                        lineStyle: {
                            normal: {
                                width: 1.5
                            }
                        },
                        data: data.data
                    }];

                option.xAxis.data = data.column;
                option.series = series;
                
                myChart.setOption(option);
            }
        })
    }



    function setOrderChart4(myChart) {

        // 指定图表的配置项和数据
        var option = {
            title: {
                text: '问题数量',
                subtext: '统计各问题类型数量'
            },
            color: [
                "#18d1b1",
                "#3fb1e3",
                "#626c91",
                "#a0a7e6",
                "#c4ebad",
                "#96dee8"
            ],
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                type: 'scroll',
                data: []
            },
            toolbox: {
                show: true,
                feature: {
                    magicType: { show: true, type: ['stack', 'tiled'] },
                    saveAsImage: { show: true }
                }
            },
            xAxis: {
                type: 'category',
                boundaryGap: true,
                data: []
            },
            yAxis: {
                type: 'value'
            },
            grid: [{
                left: 0,
                top: 60,
                right: 0,
                bottom: 30
            }],
            series: []
        };

        $.ajax({
            url: 'dashboard/messagecount',
            type: 'POST',
            dataType: 'json',
            success: function (data) {

                option.xAxis.data = data.column;
                option.series = [
                    {
                        data: data.data,
                        barWidth: '15%',
                        type: 'bar'
                    }
                ];
                
                myChart.setOption(option);
            }
        })
    }


    function setOrderChart5(myChart) {

        // 指定图表的配置项和数据
        var option = {
            title: {
                text: '销售业绩',
                subtext: '统计各销售完成订单业绩',
                top:'0px',
            },
            color: [
                "#18d1b1",
                "#3fb1e3",
                "#626c91",
                "#a0a7e6",
                "#c4ebad",
                "#96dee8"
            ],
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                type: 'scroll',
                top:'60px',
                data: []
            },
            toolbox: {
                show: true,
                feature: {
                    magicType: { show: true, type: ['stack', 'tiled'] },
                    saveAsImage: { show: true }
                }
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: []
            },
            yAxis: {},
            grid: [{
                left: 'left',
                top: 100,
                right: '10',
                bottom: 30
            }],
            series: []
        };

        $.ajax({
            url: 'dashboard/saleprice',
            type: 'POST',
            dataType: 'json',
            data:{
                between: $("#saleform").find("input[name='datetimerange']").val()
            },
            success: function (data) {
                exportDataList5 = data;
                let series = [];
                data.data.forEach(item => {
                    item.title = item.nickname;
                    option.legend.data.push(item.nickname);
                    series.push({
                        name: item.nickname,
                        type: 'line',
                        smooth: true,
                        areaStyle: {
                            normal: {}
                        },
                        lineStyle: {
                            normal: {
                                width: 1.5
                            }
                        },
                        data: item.data
                    })
                })
                option.xAxis.data = data.column;
                option.series = series;
                
                myChart.setOption(option);
            }
        })
    }


    function setOrderChart6(myChart) {

        // 指定图表的配置项和数据
        var option = {
            title: {
                text: '项目统计',
                subtext: '统计各检测项目完成订单数量',
                top:'0px'
            },
            color: [
                "#18d1b1",
                "#3fb1e3",
                "#626c91",
                "#a0a7e6",
                "#c4ebad",
                "#96dee8"
            ],
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                type: 'scroll',
                top:'60px',
                data: []
            },
            toolbox: {
                show: true,
                feature: {
                    magicType: { show: true, type: ['stack', 'tiled'] },
                    saveAsImage: { show: true }
                }
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: []
            },
            yAxis: {},
            grid: [{
                left: 'left',
                top: 100,
                right: '10',
                bottom: 30
            }],
            series: []
        };

        $.ajax({
            url: 'dashboard/goodscount',
            type: 'POST',
            dataType: 'json',
            data:{
                between: $("#goodsform").find("input[name='datetimerange']").val()
            },
            success: function (data) {
                exportDataList6 = data;
                let series = [];
                data.data.forEach(item => {
                    option.legend.data.push(item.title);
                    series.push({
                        name: item.title,
                        type: 'line',
                        smooth: true,
                        areaStyle: {
                            normal: {}
                        },
                        lineStyle: {
                            normal: {
                                width: 1.5
                            }
                        },
                        data: item.data
                    })
                })
                option.xAxis.data = data.column;
                option.series = series;
                
                myChart.setOption(option);
            }
        })
    }

    function setOrderChart7(myChart) {

        // 指定图表的配置项和数据
        var option = {
            title: {
                text: '销售订单',
                subtext: '统计各销售完成订单数',
                top:'0px',
            },
            color: [
                "#18d1b1",
                "#3fb1e3",
                "#626c91",
                "#a0a7e6",
                "#c4ebad",
                "#96dee8"
            ],
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                type: 'scroll',
                top:'60px',
                data: []
            },
            toolbox: {
                show: true,
                feature: {
                    magicType: { show: true, type: ['stack', 'tiled'] },
                    saveAsImage: { show: true }
                }
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: []
            },
            yAxis: {},
            grid: [{
                left: 'left',
                top: 100,
                right: '10',
                bottom: 30
            }],
            series: []
        };

        $.ajax({
            url: 'dashboard/saleorder',
            type: 'POST',
            dataType: 'json',
            data:{
                between: $("#saleorderform").find("input[name='datetimerange']").val()
            },
            success: function (data) {
                exportDataList7 = data;
                let series = [];
                data.data.forEach(item => {
                    item.title = item.nickname;
                    option.legend.data.push(item.nickname);
                    series.push({
                        name: item.nickname,
                        type: 'line',
                        smooth: true,
                        areaStyle: {
                            normal: {}
                        },
                        lineStyle: {
                            normal: {
                                width: 1.5
                            }
                        },
                        data: item.data
                    })
                })
                option.xAxis.data = data.column;
                option.series = series;
                
                myChart.setOption(option);
            }
        })
    }


    /**
         * 通用导出函数
         * @param {Array} data - 要导出的二维数组数据
         * @param {String} fileName - 导出文件名
         */
        function exportToExcel(data, fileName) {
            console.log(XLSX);
            
            // 创建工作表
            const worksheet = XLSX.utils.aoa_to_sheet(data);
            
            // 创建工作簿并添加工作表
            const workbook = XLSX.utils.book_new();
            XLSX.utils.book_append_sheet(workbook, worksheet, '数据');
            
            // 导出文件
            XLSX.writeFile(workbook, `${fileName}.xlsx`);
        }

    return Controller;
});
